# -*- conding:UTF-8 -*-

'''
@ Project: finance
@ File: kline.py
@ Author: 韩成瑾
@ Date：2022/10/25 15:01

'''


# from fastapi import APIRouter, WebSocket
#
# router = APIRouter()
#
#
# class WebSocketHeader:
#     ws_list = []
#
#     def __init__(self):
#         ...
#
#     async def send(self, ws, data):
#         await ws.send_text(f"Message text was: {data}")
#
#     async def broadcast(self, data):
#         for ws in self.ws_list:
#             await self.send(ws, data)
#
#     async def accept(self, ws):
#         await ws.accept()
#         self.ws_list.append(ws)
#
#
# ws_header = WebSocketHeader()
#
#
# @router.websocket("/ws")
# async def websocket_endpoint(websocket: WebSocket):
#     # 等待 websocket 连接
#     await websocket.accept()
#
#     while True:
#         # 监听接受的数据
#         data = await websocket.receive_text()
#
#         # 发送数据
#         await websocket.send_text(f"Message text was: {data}")
#
#
# @router.websocket("/ws2")
# async def websocket2_endpoint(ws: WebSocket):
#     # 等待 websocket 连接
#     await ws_header.accept(ws)
#
#     while True:
#         # 监听接受的数据
#         data = await ws.receive_text()
#
#         # 广播数据
#         await ws_header.broadcast(data)
#
#
# # @router.websocket('/{kline_id}')
# # async
import pymysql
import requests
# 1分钟
url = 'https://www.mexc.com/api/platform/spot/market/kline?end=1666955718801&interval=Min1&openPriceMode=LAST_CLOSE&start=1666946718801&symbol=DOGE_USDT'

# 15分钟
# url = 'https://www.mexc.com/api/platform/spot/market/kline?end=1666960076023&interval=Min1&openPriceMode=LAST_CLOSE&start=1666951076023&symbol=DOGE_USDT'


response = requests.get(url=url)
json_data = response.json()['data']
# print(json_data)

for i in json_data:
    open = json_data['t']  # 开
    high = json_data['h']  # 高
    down = json_data['l']  # 低
    collect = json_data['o']  # 收
    belong_time = json_data['t']  # 时间
    minute_num = json_data['v']  # 成交量
    for j in range(len(belong_time)):
        conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='password', database='finance')
        cursor = conn.cursor()
        # 1分钟
        sql = f"""insert into myapp_klinemone values (0,'抹茶',{open[j]},{high[j]},{down[j]},{collect[j]},{belong_time[j]},{minute_num[j]},'1','1')"""
        # 15分钟
        # sql = f"""insert into myapp_klinemfifteen values (0,"抹茶",'{open[j]}','{high[j]}','{down[j]}','{collect[j]}','{belong_time[j]}','{minute_num[j]}','1','1')"""
        try:
            cursor.execute(sql)
            conn.commit()
            print('插入成功')
        except Exception as e:
            print('插入失败', e)
            conn.rollback()









